#include "StdAfx.h"
#include "Sdk2000.h"

#include "highgui.h"
#include "..\..\lib\10moons\Inc\DSStream.h"


CSdk2000Cam::CSdk2000Cam(void)
{
	m_cameraId = -1;
	m_initialized = false;
}


CSdk2000Cam::~CSdk2000Cam(void)
{
}

void CSdk2000Cam::Initialize()
{
	HRESULT result = S_FALSE;

	result = DSStream_Initialize();
	if (FAILED(result))
	{
		throw "Initalize failed"; 
	}


	int cardNo = 0;
	DSStream_GetCardNumber(&cardNo);

	if (cardNo == 0)
	{
		throw "No card found";
	}

	result = DSStream_ConnectDevice(m_cameraId, true);

	if (FAILED(result))
	{
		throw "connect card failed";
	}

	m_initialized = true;
}


IplImage* CSdk2000Cam::CaptureFrame()
{
	if (!m_initialized)
	{
		throw "not initialized";
	}

	BOOL isLocked = false;
	HRESULT r = S_FALSE;
	r = DSStream_IsVideoSignalLocked(m_cameraId, &isLocked);
	if (FAILED(r))
	{
		throw "no signal";
	}

	char *fileName = "temp.jpg";

	r = DSStream_SaveToJpgFile(this->GetCameraId(), fileName, 75);
	if (FAILED(r))
	{
		throw "save file failed";
	}

	Sleep(50);

	return cvLoadImage(fileName, 1);
}
